package com.tg.app.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;




public class CircleView  extends View {
    private Paint paint;
    private int incRadius = 0;
    private boolean doAnim = true;
    public CircleView(Context context) {
        super(context);
        init(context);
    }

    public CircleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }


    private void init(Context context) {
        paint = new Paint();
        paint.setColor(Color.parseColor("#DCEDFB"));
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL);//画笔属性是实心圆
        paint.setStrokeWidth(3);//设置画笔粗细

    }



    public void startAnim(){
          doAnim = true;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    while (doAnim){
                        incRadius = incRadius + 10;
                        if (incRadius > 150){
                            incRadius = 0;
                        }
                        postInvalidate();
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }).start();
    }

    public void stopAnim(){
        doAnim = false;
        incRadius = 0;
        postInvalidate();
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        doAnim =false;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(getWidth() /2f, getHeight() / 2f,getWidth() /4f + incRadius, paint);
    }

}
